﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DADAL;

namespace DABLL
{
    public class Template
    {
        /// <summary> 添加试压模板
        /// 
        /// </summary>
        /// <param name="obj"></param>
        public static Response AddTemplate(TemplateModel obj)
        {
            Response response = Response.DefaultResponse();
            bool res = TemplateModel.RecordExistenceWithNumber(obj.Number);
            if (res)
            {
                response.Msg = string.Format("【{0}】模板已存在", obj.Number);
                return response;
            }
            List<TemplateModel> list = new List<TemplateModel>();
            list.Add(obj);
            int result = SqlSugarHelper.Insertable(list);
            if (result > 0)
            {
                response.Code = 200;
                response.Msg = string.Format("已添加【{0}】模板", obj.Number);
            }
            else
            {
                response.Msg = string.Format("模板添加失败");
            }
            return response;
        }

        /// <summary>
        /// 删除模板
        /// </summary>
        /// <param name="number">模板编号</param>
        /// <returns></returns>
        public static Response DeleteTemplate(string number)
        {
            Response response = Response.DefaultResponse();
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["number"] = number;
            List<TemplateModel> list = SqlSugarHelper.Inquireable<TemplateModel>(where);
            if (list.Count > 0)
            {
                int deleteRes = SqlSugarHelper.Deleteable(list);
                if (deleteRes > 0)
                {
                    response.Code = 200;
                    response.Msg = string.Format("模板已删除");
                }
                else
                {
                    response.Msg = string.Format("模板删除失败");
                }
                return response;
            }
            else
            {
                response.Msg = string.Format("【{0}】模板不存在，无法删除", number);
            }
            return response;
        }

        /// <summary>
        /// 分页列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public static List<TemplateModel> TemplateList(int page, int pageSize, ref int totalCount)
        {
            List<TemplateModel> list = SqlSugarHelper.InquireableToPageList<TemplateModel>(null, page, pageSize, ref totalCount);
            return list;
        }
    }
}
